package com.example.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.studyapplication.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: 葛浩杰
 * @Version: 1.0 2020/12/21
 */
public class ARGBView extends View {

    private int type;
    private Paint paint;
    private float t;
    private float startX,startY;
    private float endX,endY ;
    private Handler timerHandler;
    private Runnable runnable;
    private Path path;
    private float contorlX,contorlY;
    private float contorlX1 = 90,contorlY1 = 60;//默认值
    private float contorlX2 = 400,contorlY2 = 60;//默认值
    private List<PointF> points;//存储曲线上运动的点

    public ARGBView(Context context) {
        super(context);
    }

    public ARGBView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        timerHandler = new Handler();
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.canvas_type);
        type = ta.getInteger(R.styleable.canvas_type_type, 0);
        ta.recycle();
        paint = new Paint();
        startX = 100;
        startY = 100;
        endX = 300;
        endY = 300;
        path = new Path();
        points = new ArrayList<>();
    }

    public ARGBView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (type){
            case 0:
                canvas.drawARGB(255, 139, 197, 186);
                break;
            case 1:
//                x默认是这个字符串的左边在屏幕的位置，如果设置了paint.setTextAlign(Paint.Align.CENTER);那就是字符的中心，y是指定这个字符baseline在屏幕上的位置
                paint.setColor(getResources().getColor(R.color.purple_200));
                paint.setTextSize(20);
                canvas.drawText("绘制文本内容", 300, 300, paint);
                break;
            case 2:
                canvas.drawColor(getResources().getColor(R.color.black));
                break;
            case 3:
                paint.setColor(getResources().getColor(R.color.black));
                paint.setStrokeWidth(20);
                paint.setStrokeCap(Paint.Cap.SQUARE);//点的样式
                canvas.drawPoint(100, 100, paint);
//                float[] points = {0, 0, 50, 50, 50, 100, 100, 50, 100, 100, 150, 50, 150, 100};
// 绘制四个点：(50, 50) (50, 100) (100, 50) (100, 100)
//                canvas.drawPoints(points, 2 /* 跳过两个数，即前两个 0 */,
//                        8 /* 一共绘制 8 个数（4 个点）*/, paint);
                break;
            case 4:
                paint.setColor(getResources().getColor(R.color.black));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    //50 50 把椭圆用长方形包裹起来， 100是宽度 350高度
                    canvas.drawOval(50, 50, 100, 350, paint);
                }
                break;
            case 5:
                paint.setColor(getResources().getColor(R.color.black));
                paint.setStrokeWidth(20);
                canvas.drawLine(50, 50, 100, 350, paint);
                break;
            case 6:
                paint.setColor(getResources().getColor(R.color.black));
                canvas.drawCircle(150, 150, 50,  paint);
                break;
            case 7:
//                canvas.drawBitmap(((BitmapDrawable)getResources().getDrawable(R.mipmap.tranl)).getBitmap(), 0, 0,  paint);
                Bitmap output = Bitmap.createBitmap(200, 100, Bitmap.Config.ARGB_8888);
                output.eraseColor(Color.RED);
                canvas.drawBitmap( drawMyBitmap2(output,20), 100, 100,  paint);
                break;
            case 8:
                RectF rect = new RectF(0, 0, 100, 100);
                canvas.drawArc(rect, //弧线所使用的矩形区域大小
                        0, //开始角度
                        -150, //扫过的角度
                        true, //是否使用中心
                        paint);
                break;
            case 9:
                RectF rect1 = new RectF(50, 50, 200, 200);
//                canvas.drawRect(rect1, paint);
                canvas.drawRoundRect(rect1,
                        30, //x轴的半径
                        30, //y轴的半径
                        paint);
                break;
            case 10:
                Path path1 = new Path();
                path1.moveTo(10, 10); //移动到 坐标10,10
                path1.lineTo(50, 60);
                path1.lineTo(200,80);
                path1.lineTo(10, 10);
//                canvas.drawPath(path, paint);
                canvas.drawTextOnPath("我爱中华人民共和国", path1, 0, 0, paint);
                break;
            case 11:
                paint.setColor(Color.parseColor("#e5e5e5"));
                paint.setStrokeWidth(3);
                canvas.drawLine(startX,startY,endX,endY,paint);
                float pointX = (1-t) * startX + t*endX;
                float pointY =(1-t) * startY + t*endY;

                paint.setColor(Color.RED);
                paint.setStrokeWidth(5);
                canvas.drawLine(startX, startY, pointX, pointY, paint);
                if(t>=1.0){
                    timerHandler.removeCallbacks(runnable);//调用此方法，以关闭此定时器
                    break;
                }
                runnable = new Runnable() {
                    @Override
                    public void run() {
                        t += 0.01;
                        invalidate();
                    }
                };
                timerHandler.postDelayed(runnable,50);
                break;
            case 12:
                path.reset();
                path.moveTo(startX,startY);
                path.quadTo(contorlX,contorlY,endX,endY);
                canvas.drawPath(path,paint);
                break;
            case 13:
                if(t>=1.0f){
                    t=1.0f;
                }
                //绘制线 起点-控制点1  控制点1-控制点2  控制点2-终点
                paint.setStrokeWidth(4);
                paint.setColor(Color.parseColor("#e5e5e5"));
                canvas.drawLine(startX,startY,contorlX1,contorlY1,paint);
                canvas.drawLine(contorlX1,contorlY1,contorlX2,contorlY2,paint);
                canvas.drawLine(contorlX2,contorlY2,endX,endY,paint);
                //绘制轨迹直线
                float p3x = (1 - t) * startX+ t* contorlX1;
                float p3y = (1 - t) *startY + t * contorlY1;
                float p4x = (1 - t) * contorlX1 + t * contorlX2;
                float p4y = (1 - t) * contorlY1 + t * contorlY2;
                float p6x = (1 - t) * contorlX2 + t * endX;
                float p6y = (1 - t) * contorlY2 + t * endY;

                float pc7x = (1 - t) * p3x + t * p4x;
                float pc7y = (1 - t) * p3y + t * p4y;

                float pc8x = (1 - t) * p4x + t * p6x;
                float pc8y = (1 - t) * p4y + t * p6y;
                paint.setColor(Color.GREEN);
                //绘制曲线运动轨迹
                canvas.drawLine(p3x, p3y, p4x, p4y, paint);
                canvas.drawLine(p4x, p4y, p6x, p6y, paint);
                paint.setColor(Color.BLUE);
                //绘制辅助线
                canvas.drawLine(pc7x, pc7y, pc8x, pc8y, paint);

                float p9x = (1 - t) * pc7x + t * pc8x;
                float p9y = (1 - t) * pc7y + t * pc8y;
                points.add(new PointF(p9x, p9y));
                paint.setColor(Color.RED);
                PointF ps, pe;
                for (int i = 1; i < points.size(); i++) {//其实这个二阶曲线就是由无数个点构成,然后把前后2个点构成一条直线
                    ps = points.get(i - 1);
                    pe = points.get(i);
                    canvas.drawLine(ps.x, ps.y, pe.x, pe.y, paint);//绘制贝塞尔在时间0到1内曲线运动轨迹
                }
                paint.setColor(Color.parseColor("#e5e5e5"));
                paint.setStrokeWidth(6);
                paint.setColor(Color.BLUE);
                canvas.drawPoint(contorlX1,contorlY1,paint);
                canvas.drawPoint(contorlX2,contorlY2,paint);
                path.reset();
                if(t==1.0f){
                    path.moveTo(startX,startY);
                    path.cubicTo(contorlX1,contorlY1,contorlX2,contorlY2,endX,endY);
                    canvas.drawPath(path,paint);
                }
                postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        t+=0.01;
                        invalidate();
                    }
                },50);
                break;
            case 14:
                paint.setAntiAlias(true);
                paint.setStyle(Paint.Style.STROKE);
                canvas.translate(getWidth()/2, 200); // 参数1: 向X轴方向移动距离  参数2: 向Y轴方向移动距离
                paint.setColor(getResources().getColor(R.color.white));
                canvas.drawRect(0,0,100,100,paint);
                paint.setColor(getResources().getColor(R.color.black));
                canvas.drawCircle(0, 0, 100, paint); //画圆圈
                //使用path绘制路径文字
                canvas.save();
                canvas.translate(-75, -75);
                paint.setColor(getResources().getColor(R.color.white));
                canvas.drawRect(0,0,150,150,paint);
                Path path = new Path();
                path.addArc(new RectF(0,0,150,150), -180, 180);
                paint.setColor(getResources().getColor(R.color.black));
                Paint citePaint = new Paint(paint);
                citePaint.setTextSize(14);
                citePaint.setStrokeWidth(1);
                canvas.drawTextOnPath("http://www.android777.com", path, 28, 0, citePaint);
                canvas.restore();
                Paint tmpPaint = new Paint(paint); //小刻度画笔对象
                tmpPaint.setStrokeWidth(1);
                float y=100;
                int count = 60; //总刻度数
                for(int i=0 ; i <count ; i++){
                    if(i%5 == 0){
                        canvas.drawLine(0f, y, 0, y+12f, paint);
                        canvas.drawText(String.valueOf(i/5+1), -4f, y+25f, tmpPaint);
                    }else{
                        canvas.drawLine(0f, y, 0f, y +5f, tmpPaint);
                    }
                    canvas.rotate(360/count,0f,0f); //旋转画纸
                }
                //绘制指针
                tmpPaint.setColor(Color.GRAY);
                tmpPaint.setStrokeWidth(4);
                canvas.drawCircle(0, 0, 7, tmpPaint);
                tmpPaint.setStyle(Paint.Style.FILL);
                tmpPaint.setColor(Color.YELLOW);
                canvas.drawCircle(0, 0, 5, tmpPaint);
                canvas.drawLine(0, 10, 0, -65, paint);

                break;
            default:
                break;
        }
    }

    private Bitmap drawMyBitmap(Bitmap bitmap, int roundPx) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
//        paint.setColor(color);
//        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        Bitmap bitmap1 = ((BitmapDrawable)getResources().getDrawable(R.mipmap.tranl)).getBitmap();
        // 计算缩放比例
        float scaleWidth = ((float) bitmap.getWidth()) / bitmap1.getWidth();
        float scaleHeight = ((float) bitmap .getHeight()) / bitmap1.getHeight();
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        canvas.drawBitmap(bitmap1, matrix, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;

    }

    private Bitmap drawMyBitmap2(Bitmap bitmap, int roundPx) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final Paint paint = new Paint();
        paint.setAntiAlias(true);

        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);

        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        final Rect block = new Rect(0, 0, 20, 20);
        canvas.drawRect(block, paint);
        final Rect block2 = new Rect(0, 100 - 20, 20, 100);
        canvas.drawRect(block2, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        paint.setAlpha(0x40);
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_MOVE){
            contorlX = event.getX();
            contorlY = event.getY();
            invalidate();
        }
        return true;
    }

    public void setType(int type) {
        this.type = type;
    }
}
